﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class TreeViewForm : Form, ViewForm
    {
        filtr myfiltr = filtr.all;
        Form1 par;
        int size;
        public Form1.addDataHandler aH;
        public Form1.replaceDataHandler rpH;
        public Form1.removeDataHandler rmH;

        public delegate void filtrChangedHandler(ViewForm vf);

        public event filtrChangedHandler filtrChangedEvent;

        public delegate void replaceShowHandler(Data d);
        public event replaceShowHandler replaceShowEvent;
        public delegate void removeShowHandler(Data d);
        public event removeShowHandler removeShowEvent;

        public TreeViewForm(Form1 _par)
        {

            InitializeComponent();
            all.Checked = true;
            size = 0;
            par = _par;
            filtrChangedEvent += new filtrChangedHandler(par.filtrChanged);
            replaceShowEvent += new replaceShowHandler(par.replaceShow);
            removeShowEvent += new removeShowHandler(par.removeShow);
        }
        bool acceptData(Data data)
        {

            if (!(myfiltr == filtr.all ||
                (myfiltr == filtr.smaller && data.x < 0) ||
                (myfiltr == filtr.bigger && data.x >= 0)))
                return false;
            return true;
        }

        public void addItemData(Data data)
        {
            Console.WriteLine("add1 " + data.x + " "+ myfiltr);
            if (!acceptData(data))
                return;
            Console.WriteLine("add2"+data.x);
            TreeNode newNode = new TreeNode(data.label);
            newNode.Tag = data;
            TreeNode newNodeX = new TreeNode(data.x.ToString());
            newNodeX.Tag = data;
            newNode.Nodes.Add(newNodeX);
            TreeNode newNodeY = new TreeNode(data.y.ToString());
            newNodeY.Tag = data;
            newNode.Nodes.Add(newNodeY);
            TreeNode newNodeC = new TreeNode(data.color.ToString());
            newNodeC.Tag = data;
            newNode.Nodes.Add(newNodeC);

            Console.WriteLine("w1" );
            treeView1.Nodes.Add(newNode);
            Console.WriteLine("w2" );
            size++;
            statusLabel1.Text = size.ToString();
        }


        public void removeItemData(Data data)
        {
            Console.WriteLine("vntreo");
            foreach (TreeNode nd in treeView1.Nodes)
            {
                if (nd == null)
                    continue;
                Console.WriteLine("vnt hand reo");
                if ((Data)nd.Tag == data)
                {
                    treeView1.Nodes.Remove(nd); 
                    size--;
                    statusLabel1.Text = size.ToString();
                }
            }
           // 
           // TreeNodeCollection myNodeCollection = treeView1.Nodes;
           //// treeView1.
           // System.Collections.IEnumerator myEnumerator = myNodeCollection.GetEnumerator();
           // while (myEnumerator.MoveNext())
           // {
           //     if (((TreeNode)myEnumerator.Current).Text == data.label)
           //     {
           //         treeView1.Nodes.Remove((TreeNode)myEnumerator.Current);
           //         size--;
           //         statusLabel1.Text = size.ToString();
           //         break;
           //     }
           // }
        }
        public void replaceItemData(Data d)
        {
            bool found = false;
            Console.WriteLine("replace enent");
            foreach (TreeNode nd in treeView1.Nodes)
            {
                Console.WriteLine("vnt hand reo");
                if ((Data)nd.Tag == d)
                {
                    found = true;
                    if (!acceptData(d))
                    {
                        removeItemData(d);
                    }
                    else
                    {
                        nd.Text = d.label;
                        nd.FirstNode.Text = d.x.ToString();
                        nd.FirstNode.NextNode.Text = d.y.ToString();
                        nd.FirstNode.NextNode.NextNode.Text = d.color.ToString();
                    }
                }
            }
            if (!found && acceptData(d))
            {
                addItemData(d);
            }
        }

        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {

        }

        private void toolStripButton1_Click(object sender, EventArgs e)
        {

        }
        public ToolStrip ChildToolStrip
        {
            get { return toolStrip1; }
            set { toolStrip1 = value; }
        }

        public StatusStrip ChildStatusStrip
        {
            get { return statusStrip1; }
            set { statusStrip1 = value; }
        }

        private void toolStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void toolStrip1_ItemClicked_1(object sender, ToolStripItemClickedEventArgs e)
        {

        }
        private void all_Click(object sender, EventArgs e)
        {
            setFiltr(filtr.all);
        }

        private void small_Click(object sender, EventArgs e)
        {
            setFiltr(filtr.smaller);
        }

        private void big_Click(object sender, EventArgs e)
        {
            setFiltr(filtr.bigger);
        }
        public void setFiltr(filtr f)
        {
            if (myfiltr == f)
                return;
            switch (f)
            {
                case filtr.all:
                    myfiltr = filtr.all;
                    all.Checked = true;
                    small.Checked = false;
                    big.Checked = false;
                    break;
                case filtr.smaller:
                    myfiltr = filtr.smaller;
                    all.Checked = false;
                    small.Checked = true;
                    big.Checked = false;
                    break;
                case filtr.bigger:
                    myfiltr = filtr.bigger;
                    all.Checked = false;
                    small.Checked = false;
                    big.Checked = true;
                    break;
            }
            treeView1.Nodes.Clear();
            size = 0;
            statusLabel1.Text = size.ToString();
            filtrChangedEvent(this);

        }

        private void treeView1_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
                treeView1.SelectedNode = e.Node;
                if (treeView1.SelectedNode != null)
                {
                    replaceToolStripMenuItem.Enabled = true;
                    removeToolStripMenuItem.Enabled = true;
                    removeToolStripMenuItem.Enabled = true;
                    replaceToolStripMenuItem.Enabled = true;
                    removeToolStripMenuItem1.Enabled = true;
                    replaceToolStripMenuItem1.Enabled = true;
                }
        }

        private void editToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Data d = (Data)treeView1.SelectedNode.Tag;
            replaceShowEvent(d);
        }

        private void removeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Data d = (Data)treeView1.SelectedNode.Tag;
            removeShowEvent(d);
        }

        private void treeView1_AfterSelect_1(object sender, TreeViewEventArgs e)
        {
            if (treeView1.SelectedNode != null)
            {
                toolStripButtonRemove.Enabled = true;
                toolStripButtonReplace.Enabled = true;
                removeToolStripMenuItem.Enabled = true;
                replaceToolStripMenuItem.Enabled = true;
                removeToolStripMenuItem1.Enabled = true;
                replaceToolStripMenuItem1.Enabled = true;
            }
        }

        private void TreeViewForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (!par.closeChild(this))
                e.Cancel = true;
        }

        private void addToolStripMenuItem_Click(object sender, EventArgs e)
        {
            par.addToolStripMenuItem_Click(sender, e);
        }

        private void TreeViewForm_FormClosed(object sender, FormClosedEventArgs e)
        {
            par.addDataEvent -= aH;
            par.removeDataEvent -= rmH;
            par.replaceDataEvent -= rpH;

        }
        
    }
}
